<template>
  <div>
    <el-dialog :title="obj.isadd?'添加':'修改'" :visible.sync="obj.isshow" @closed="closed">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="所属角色">
          <el-select v-model="form.roleid" placeholder="请选择">
            <el-option
              :label="item.rolename"
              :value="item.id"
              v-for="item in roledata"
              :key="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户名">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="form.password"></el-input>
          <p>如果不填写密码则不修改</p>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch
            v-model="form.status"
            active-color="#13ce66"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="add">{{obj.isadd?'添加':'修改'}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { reqUseradd, reqUserinfo, reqUseredit } from "../../../request/api.js";
import { successAlert } from "../../../utils/alert";
import {mapGetters,mapActions} from "vuex"
export default {
  props: ["obj", "roledata"],
  data() {
    return {
      dialogFormVisible: false,
      form: {
        roleid: "",
        username: "",
        password: "",
        status: ""
      }
    };
  },
  computed:{
    ...mapGetters({
      user:"user"
    })
  },
  methods: {
    ...mapActions({
      changeuser:"changeuser"
    }),
    isshow() {
      this.isshow = true;
    },
    add() {
      if (this.obj.isadd) {
        reqUseradd(this.form).then(res => {
          if (res.data.code == 200) {
            //成功了
            // （1）弹成功
            successAlert(res.data.msg);
            // (2)弹框消失
            this.cancel();
            // (3)清空数据
            this.form = {
              roleid: "",
              username: "",
              password: "",
              status: ""
            };
            // (4)刷新列表
            this.$emit("refresh");
          }
        });
      } else {
        reqUseredit(this.form).then(res => {
          if (res.data.code == 200) {
            // （1）弹成功
            successAlert(res.data.msg);
          }
          //如果修改的管理就是当前登录的账号，那么需要重新登录
          if (this.user.uid === this.form.uid) {
            //清除用户数据
            this.changeuser({});
            //退出登录
            this.$router.replace("/");
            return;
          }
          // (2)弹框消失
            this.cancel();
            // (3)清空数据
            this.form = {
              roleid: "",
              username: "",
              password: "",
              status: ""
            };
            // (4)刷新列表
            this.$emit("refresh");
        });
      }
    },
    change(uid) {
      reqUserinfo({ uid: uid }).then(res => {
        res.data.list.password = "";
        this.form = res.data.list;
      });
    },
    cancel() {
      this.obj.isshow = false;
      if (!this.obj.isadd) {
        this.form = {
          roleid: "",
          username: "",
          password: "",
          status: ""
        };
      }
    },
    closed() {
      if (!this.obj.isadd) {
        this.form = {
          roleid: "",
          username: "",
          password: "",
          status: ""
        };
      }
    }
  }
};
</script>

<style>
</style>